

/**************************************************START OF FILE*****************************************************/




/*------------------------------------------------------------------------------------------------------------------
Includes
*/
#include "xBsp_Hcs301.h"
#include "stm32f0xx_hal.h"
#include "stdint.h"
#include "xBSP_FLASH.h"

/*------------------------------------------------------------------------------------------------------------------
Definitions
*/


/*------------------------------------------------------------------------------------------------------------------
Variables
*/

uint8_t SendBuffer[64];

uint8_t HcsxxWriteFlag = 0;				//在线烧录请求
uint8_t OffLine_HcsxxWriteFlag = 0;		//脱机烧录请求
uint8_t PCWriteConfigFlag = 0;			//接收到写入配置请求

uint8_t isSendReport = 0;       		//标记是否要发送报告。

//用来判断写数据是否完成
extern uint8_t isWriteFinish;
//判断写入数据是否Ok
extern uint8_t isWriiteOk; 

//脱机烧录信息
uint16_t OffLine_WriteDataBuffer[16] = 	//写入的数据
{
	0x1234,		//key_0
	0x5678, 	//key_1
	0x1234, 	//key_2
	0x5678,		//key_3
	0xA000,		//SYNC
	0x0000,		//RES
	0xABCD,		//SER_0 序列号	//6
	0x8234,		//SER_1	序列号	//7
	0xFFFF, 	//SEED_0 种子	//8
	0xFFFF,		//SEED_1 种子	//9
	0x0000, 	//RES
	0x1FFF,		//CONFIG		//11
	0x0000,		//模式0：简单模式 1:正常模式 2：安全模式	  //12
	0x0000,		//种子值自动加一							//13
	0x0000,		//序列号自动加一							//14
	0x0000,		//识别码取自序列号							//15
};

//写入到芯片的数据
extern uint16_t WriteDataBuffer[12];


/*------------------------------------------------------------------------------------------------------------------
Code
*/
extern uint8_t USBD_HID_SendReport(uint8_t *report, uint16_t len);
void xAPP_HCS_ChangeDownloadData(void);




/*
*********************************************************************************************************************

@ Brief  : 应用部分的入口函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ:872180981)

@ Data   : 2020-08-04 15:35

*********************************************************************************************************************
*/
void XAPP_Main(void)
{


    while (1)
    {
        //执行开始烧录命令
        if(HcsxxWriteFlag && (isWriteFinish == 1))
        {
            xBSP_HCS301_StartProgram();
            HcsxxWriteFlag = 0;
            //标记要返回数
            isSendReport = 1;
        }
		else
		{
			HcsxxWriteFlag = 0;
		}
		

		//脱机烧录方式
		if(OffLine_HcsxxWriteFlag && (isWriteFinish == 1))
        {
			//拿到数据
			xAPP_HCS_ChangeDownloadData();
			//启动烧录
            xBSP_HCS301_StartProgram();
            OffLine_HcsxxWriteFlag = 0;
        }
		else
		{
			OffLine_HcsxxWriteFlag = 0;
		}
		

		


		//PC端写入配置参数（保存到Flash）
		if(PCWriteConfigFlag)
		{
			PCWriteConfigFlag = 0;
			xBSP_FLASH_SaveData((uint32_t*)OffLine_WriteDataBuffer , 30);
		}



		//返回参数给PC
        if(isSendReport == 1)
        {
            if(isWriteFinish == 1)
            {
                isSendReport = 0;
                SendBuffer[0] = 0xAA;
                SendBuffer[1] = 0x55;
                SendBuffer[62] = 0x55;
                SendBuffer[63] = 0xAA;
                SendBuffer[2] = 0xE1;
                SendBuffer[3] = isWriiteOk;
                USBD_HID_SendReport(SendBuffer , 0x40);
            }
        }
    }

}




/*
*********************************************************************************************************************

@ Brief  : USB HID设备接收中断

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ:872180981)

@ Data   : 2020-08-25 15:39

*********************************************************************************************************************
*/
void USB_RxData(uint8_t* RxData)
{

    uint16_t CheckSum = 0;
    uint16_t RxCheckSum = 0;
    //判断头跟尾
    if((RxData[0] == 0xAA)  && (RxData[1] == 0x55) && (RxData[62] == 0x55) && (RxData[63] == 0xAA))
    {
        //计算checksum
        for (int i = 0; i < 58; i++)
        {
            CheckSum += RxData[i+ 2];
        }

        //接收到的checksum
        RxCheckSum = RxData[60];
        RxCheckSum <<= 8;
        RxCheckSum |= RxData[61];
        
        if(RxCheckSum == CheckSum)
        {
            switch (RxData[2])
            {
                //写HCS301
                case 0xF1:
                    for (int i = 0; i < 12; i++)
                    {
                        WriteDataBuffer[i] = 0;
                        WriteDataBuffer[i] = RxData[3 + 2 * i]; 
                        WriteDataBuffer[i] <<= 8;
                        WriteDataBuffer[i] |= RxData[4 + 2 * i]; 
                    }
                    HcsxxWriteFlag = 1;
                break;
                //脱机写入数据
                case 0xF2:

					for (int i = 0; i < 16; i++)
					{
						OffLine_WriteDataBuffer[i] = 0;
						OffLine_WriteDataBuffer[i] = RxData[3 + 2 * i]; 
						OffLine_WriteDataBuffer[i] <<= 8;
						OffLine_WriteDataBuffer[i] |= RxData[4 + 2 * i]; 
					}
					//通知保存数到flash
					PCWriteConfigFlag = 1;
                break;
            }
        }
    }
}


/*
*********************************************************************************************************************
@ Brief  : 改变烧录到芯片的数据
@ Param  : NONE
@ Return : NONE
@ Author : YWJ(QQ:872180981)
@ Data   : 2020-12-02 16:13
*********************************************************************************************************************
*/
void xAPP_HCS_ChangeDownloadData(void)
{

	uint32_t CurrentInedx;
	uint32_t temp = 0;
	uint32_t temp2 = 0;
	CurrentInedx = xBSP_FLASH_ReadWriteIndex();


	//读取配置
	xBSP_FLASH_ReadLastPage((uint32_t*)OffLine_WriteDataBuffer , 16);

	
	//序列号自动加一
	if(OffLine_WriteDataBuffer[14])
	{
		//序列号 只用28bit
		temp = OffLine_WriteDataBuffer[6];
		temp |= OffLine_WriteDataBuffer[7] << 16;
		temp2 = (temp & 0x80000000);	//保留最高位
		temp &= 0xFFFFFFF;
		//循环处理
		temp += (CurrentInedx & 0xFFFFFFF);
		//溢出处理
		temp &= 0xFFFFFFF;
		temp |= temp2;	//保留最高位
		OffLine_WriteDataBuffer[6] = temp;
		OffLine_WriteDataBuffer[7] = temp>>16;
	}

	//种子值自动加一
	if(OffLine_WriteDataBuffer[13])
	{
		temp = OffLine_WriteDataBuffer[8];
		temp |= OffLine_WriteDataBuffer[9] << 16;
		temp += CurrentInedx;
		OffLine_WriteDataBuffer[8] = temp;
		OffLine_WriteDataBuffer[9] = temp>>16;

	}

	//识别码取自序列号
	if(OffLine_WriteDataBuffer[15])
	{
        temp = OffLine_WriteDataBuffer[6] | (OffLine_WriteDataBuffer[7]<<16);
        temp &= 0x3FF;
		OffLine_WriteDataBuffer[11] &= 0xFC00;  		//低10位清零
		OffLine_WriteDataBuffer[11] |= temp;   			//改变值

	}

	//加密模式
	// if(OffLine_WriteDataBuffer[12])
	// {

	// }

	//复制到写入缓冲区
	for(uint8_t i = 0 ; i < 12 ; i++)
	{
		WriteDataBuffer[i] = OffLine_WriteDataBuffer[i];
	}
}


/**************************************************END OF FILE*******************************************************/

